const jwt = require('jsonwebtoken')
const key = '0215最牛逼'
//生成token函数
const signToken = ({username,userId},expiresIn='1d')=>{
    const token =jwt.sign(
        {data:{username,userId}},
        key,
        {expiresIn}
    )
    return token
}


//验证token函数
const verifyToken = (token) => {
    return new Promise((resolve, reject) => {
      jwt.verify(token, key, (error, decode) => {
        if(error) {
          reject(new Error('token过期或非法'))
        } else {
          resolve(decode.data)
        }
      })
    })
  }
// console.log(verifyToken(token));

module.exports={
    signToken,
    verifyToken
}